import 'package:flutter/material.dart';

class StatefulDemo extends StatefulWidget {
  final String? title;

  StatefulDemo({super.key, required this.title}) {
    print('Widget构造函数被调用了!');
  }

  @override
  State<StatefulDemo> createState() {
    ///不要在createState中写逻辑
    print('createState被调用了！');
    return _StatefulDemoState();
  }
}

class _StatefulDemoState extends State<StatefulDemo> {
  int count = 0;

  _StatefulDemoState() {
    print('State 构造方法被调用了！');
  }

  @override
  void initState() {
    super.initState();
    print('State initState 方法被调用了！');
  }

  @override
  Widget build(BuildContext context) {
    print('State build 方法被调用了！');
    return Scaffold(
      appBar: AppBar(
        title: Text('StateFul'),
        centerTitle: true,
      ),
      body: Column(
        children: [
          ElevatedButton(
            onPressed: () {
              count++;
              setState(() {});

              ///setState的内部构造。
              // context as StatefulElement;
              // context.markNeedsBuild();
            },
            child: const Icon(Icons.add),
          ),
          Text('$count')
        ],
      ),
    );
  }

  @override
  void dispose() {
    print('State dispose方法被调用了！');
    super.dispose();
  }

  ///build之前调用（改变依赖关系 ）
  @override
  void didChangeDependencies() {
    print('State 方法被调didChangeDependencies用了！');
    super.didChangeDependencies();
  }

  ///将要被销毁
  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
  }
}
